library(tidyverse)
set.seed(289428)
R <- 1000
N <- 501
theta.0 <- c(2, -0.8, 3.5)
theta1 <- rep(0, R)
theta2 <- rep(0, R)
theta3 <- rep(0, R)
theta1m <- rep(0, R)
theta2m <- rep(0, R)
theta3m <- rep(0, R)
ci <- rep(0, R)
elt <- rep(0, R)
###estimating equations
fct <- function(theta, x)
{
  cbind(
    (x[1] - x[2]*exp(theta[1]+theta[2]*x[2])/(1+exp(theta[1]+theta[2]*x[2])) - theta[3]) * exp(theta[1]+theta[2]*x[2])/(1+exp(theta[1]+theta[2]*x[2]))^2 * x[2],
    (x[1] - x[2]*exp(theta[1]+theta[2]*x[2])/(1+exp(theta[1]+theta[2]*x[2])) - theta[3]) * exp(theta[1]+theta[2]*x[2])/(1+exp(theta[1]+theta[2]*x[2]))^2 * x[2]^2,
    x[1] - x[2]*exp(theta[1]+theta[2]*x[2])/(1+exp(theta[1]+theta[2]*x[2])) - theta[3]
  )
}
###likelihood functions
likeli.fix <- function(theta)
{
  logL<- 0
  b1 <- theta[1]
  b2 <- theta[2]
  l <- theta[3]
  for (t in 2:N)
  {
    m <- 0:y[t]
    if (y[t-1] == 0)
      temp <- dpois(y[t], l)
    else
      temp <- sum(dpois(m, y[t-1] * exp(b1+b2*y[t-1]) / (1 + exp(b1+b2*y[t-1]))) * dpois(y[t]-m, l))
    logL <- logL + log(temp)
  }
  -logL
}
likeli.unif <- function(theta)
{
  logL<- 0
  b1 <- theta[1]
  b2 <- theta[2]
  l <- theta[3]
  for (t in 2:N)
  {
    m <- 0:y[t]
    gamma1 <- expint::gammainc(m+1, 2*y[t-1]*exp(b1+b2*y[t-1]) / (1 + exp(b1+b2*y[t-1])))
    gamma2 <- gamma(m+1)
    if (y[t-1] == 0)
      temp <- dpois(y[t], l)
    else
      temp <- sum(dpois(y[t]-m, l) / (y[t-1]*factorial(m) * (2 * exp(b1+b2*y[t-1]) / (1 + exp(b1+b2*y[t-1])))) * (gamma2 - gamma1)) 
    logL <- logL + log(temp)
  }
  -logL
}
likeli.exp <- function(theta)
{
  logL<- 0
  b1 <- theta[1]
  b2 <- theta[2]
  l <- theta[3]
  for (t in 2:N)
  {
    m <- 0:y[t]
    if (y[t-1] == 0)
      temp <- dpois(y[t], l)
    else
    {
      v = (1 + exp(b1+b2*y[t-1])) / exp(b1+b2*y[t-1])
      temp <- sum(v / (v+y[t-1])^(m+1) * y[t-1]^m * dpois(y[t]-m, l))
    }
    logL <- logL + log(temp)
  }
  -logL
}
likeli.chi <- function(theta)
{
  logL<- 0
  b1 <- theta[1]
  b2 <- theta[2]
  l <- theta[3]
  for (t in 2:N)
  {
    m <- 0:y[t]
    if (y[t-1] == 0)
      temp <- dpois(y[t], l)
    else
    {
      v = exp(b1+b2*y[t-1]) / (1 + exp(b1+b2*y[t-1]))
      temp <- sum(y[t-1]^m / factorial(m) * dpois(y[t]-m, l) * gamma((v+2*m)/2) /2^(v/2) / gamma(v/2) / (0.5+y[t-1])^((v+2*m)/2))
    }
    logL <- logL + log(temp)
  }
  -logL
}
###CLS
target <- function(theta)
{
  b1 <- theta[1]
  b2 <- theta[2]
  l <- theta[3]
  sum((y1 - y0 * exp(b1+b2*y0)/(1 + exp(b1+b2*y0)) - l)^2)
}
###choose likelihood
likeli <- likeli.fix
###start simulation
for (k in 1:R)
{
  y <- rep(0, N)
  ###选择lanbda
  #z <- rgeom(N, 1/(1+theta.0[3]))
  z <- rpois(N, theta.0[3])
  y[1] <- 2
  
  for (i in 2:N)
  {
    #phi <- runif(1, min=0, max=2*exp(theta.0[1]+theta.0[2]*y[i-1])/(1+exp(theta.0[1]+theta.0[2]*y[i-1])))
    #phi <- rexp(1, (1+exp(theta.0[1]+theta.0[2]*y[i-1]))/exp(theta.0[1]+theta.0[2]*y[i-1]))
    #phi <- rchisq(1, exp(theta.0[1]+theta.0[2]*y[i-1])/(1+exp(theta.0[1]+theta.0[2]*y[i-1])))
    phi <- exp(theta.0[1]+theta.0[2]*y[i-1])/(1+exp(theta.0[1]+theta.0[2]*y[i-1]))
    y[i] <- rpois(1, phi*y[i-1]) + z[i]
  }
  
  y1 <- y[2:N]
  y0 <- y[1:(N-1)]
  
  fit.cls <- optim(theta.0 ,target, hessian = T)
  #theta.fit <- c(fit.cls$par[1], fit.cls$par[2], fit.cls$par[3])
  theta1[k] <- fit.cls$par[1]
  theta2[k] <- fit.cls$par[2]
  theta3[k] <- fit.cls$par[3]
  
  fit.cml <- optim(theta.0, likeli)
  theta1m[k] <- fit.cml$par[1]
  theta2m[k] <- fit.cml$par[2]
  theta3m[k] <- fit.cml$par[3] 
  
  dat <- data.frame(y1 = y1, y0 = y0)
  
  temp <- as.matrix(fct(theta.0, dat))
  eq <- apply(temp, 2, sum)
  vc <- t(temp) %*% temp
  tc <- t(eq) %*% solve(vc) %*% eq 
  ci[k] <- (tc <= qchisq(df=3, p = 0.95))
  
  theta.t <-c(2, 0, 3.5)
  temp <- as.matrix(fct(theta.t, dat))
  eq <- apply(temp, 2, sum)
  vc <- t(temp) %*% temp
  tel <- t(eq) %*% solve(vc) %*% eq 
  elt[k] <- (tel > qchisq(df=3, p = 0.95))
}

mean(theta1)-theta.0[1]
mean(theta2)-theta.0[2]
mean(theta3)-theta.0[3]
mean(theta1m)-theta.0[1]
mean(theta2m)-theta.0[2]
mean(theta3m)-theta.0[3]
var(theta1)
var(theta2)
var(theta3)
var(theta1m)
var(theta2m)
var(theta3m)
sqrt(mean((theta1-theta.0[1])^2))
sqrt(mean((theta2-theta.0[2])^2))
sqrt(mean((theta3-theta.0[3])^2))
sqrt(mean((theta1m-theta.0[1])^2))
sqrt(mean((theta2m-theta.0[2])^2))
sqrt(mean((theta3m-theta.0[3])^2))
mean(abs((theta1-theta.0[1])/theta.0[1]))
mean(abs((theta2-theta.0[2])/theta.0[2]))
mean(abs((theta3-theta.0[3])/theta.0[3]))
mean(abs((theta1m-theta.0[1])/theta.0[1]))
mean(abs((theta2m-theta.0[2])/theta.0[2]))
mean(abs((theta3m-theta.0[3])/theta.0[3]))
sum(ci)
sum(elt)
